<template>
  <basic-container>
    <!-- <el-alert
      title="统计时间：可查询最近三个月的信息"
      type="warning"
      :closable="false"
      style="margin-bottom: 10px"
    >
    </el-alert> -->
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
    >
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-view"
          size="small"
          type="text"
          @click.stop="handleView(scope.row, scope.index)"
          >详情</el-button
        >
      </template>
      <template slot="menuLeft">
        <div class="countcard">
          <div class="countcard-box">
            <div class="countcard-box-title">数据统计</div>
            <div class="countcard-box-content">
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">金额汇总(元)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.totalAmount) || 0
                }}</div>
              </div>
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">笔数汇总(笔)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.totalCount) || 0
                }}</div>
              </div>
            </div>
          </div>
          <div class="countcard-box">
            <div class="countcard-box-title">分账订单</div>
            <div class="countcard-box-content">
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">分账金额(元)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionAmount) || 0
                }}</div>
              </div>
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">分账笔数(笔)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionCount) || 0
                }}</div>
              </div>
            </div>
          </div>
          <div class="countcard-box">
            <div class="countcard-box-title">分账退款</div>
            <div class="countcard-box-content">
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">退款金额(元)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionRefundAmount) || 0
                }}</div>
              </div>
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">退款笔数(笔)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionRefundCount) || 0
                }}</div>
              </div>
            </div>
          </div>
          <div class="countcard-box">
            <div class="countcard-box-title">分账退回</div>
            <div class="countcard-box-content">
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">退回金额(元)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionRollbackAmount) || 0
                }}</div>
              </div>
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">退货笔数(笔)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionRollbackCount) || 0
                }}</div>
              </div>
            </div>
          </div>
          <div class="countcard-box">
            <div class="countcard-box-title">分账撤销</div>
            <div class="countcard-box-content">
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">撤销金额(元)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionCancelAmount) || 0
                }}</div>
              </div>
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">撤销笔数(笔)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionCancelCount) || 0
                }}</div>
              </div>
            </div>
          </div>
          <div class="countcard-box">
            <div class="countcard-box-title">其他</div>
            <div class="countcard-box-content">
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">分账成功金额(元)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionSuccessAmount) || 0
                }}</div>
              </div>
              <div class="countcard-box-item">
                <div class="countcard-box-item-title">到账成功金额(元)</div>
                <div class="countcard-box-item-number">{{
                  formatAmount(countData.divisionArrivalAmount) || 0
                }}</div>
              </div>
            </div>
          </div>
        </div>
      </template>
      <template slot="menuRight">
        <el-button type="primary" size="small" @click="setTime('今天')"
        >今天
        </el-button>
        <el-button type="primary" size="small" @click="setTime('昨天')"
        >昨天
        </el-button>
        <el-button type="primary" size="small" @click="setTime('本月')"
        >本月
        </el-button>
        <el-button type="primary" size="small" @click="setTime('导出')"
        >导出
        </el-button>
      </template>
      <template slot-scope="{}" slot="serviceNoSearch">
        <select-page
          ref="searchSerSelect"
          label="serviceName"
          value="serviceNo"
          :select.sync="search.serviceNo"
          :url="website.searchLink.serviceUrl"
          placeholder-text="服务商"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="rpiNoSearch">
        <select-page
          ref="searchRpiSelect"
          label="rpiName"
          value="rpiNo"
          :select.sync="search.rpiNo"
          :url="website.searchLink.merchantUrl"
          placeholder-text="商户"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="projectCodeSearch">
        <select-page
          ref="searchProjectSelect"
          label="projectName"
          value="projectCode"
          :select.sync="search.projectCode"
          :url="'division/division_project/pageList'"
          placeholder-text="项目"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="recRpiNoSearch">
        <select-page
          ref="searchRecrpiSelect"
          label="rpiName"
          value="rpiNo"
          :select.sync="search.recRpiNo"
          url="division/division_receipt/pageList"
          placeholder-text="分账接收人"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="aisleCodeSearch">
        <select-page
          ref="searchAisSelect"
          label="aisleName"
          value="aisleCode"
          :select.sync="search.aisleCode"
          :url="'aisle/rbp_pay_aisle/page'"
          placeholder-text="通道"
          filter-key="searchParam"
        ></select-page>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, getListCount, getExport } from "@/api/rb-oem/separateFunds/separate_order";
import { mapGetters } from "vuex";
import { dateFormat } from "@/util/date";
import { downloadFileExcel } from "@/util/util";
import selectPage from "@/components/select-page/select-page";

export default {
  components: {
    selectPage
  },
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        // selection: true,
        searchMenuSpan: 24,
        columnBtn: false,
        labelWidth: 170, //弹出表单的标题的统一宽度
        searchLabelWidth: 110, //搜索框标题宽度
        menu: true, // 修改为true以显示菜单
        menuWidth: 100, //操作栏宽度
        emptyBtnText:'重置',
        column: [
          {
            label: "分账接收人编号",
            searchLabel: "分账接收人",
            prop: "recRpiNo",
            type: "input",
            search: true,
            searchOrder: 2,
            overHidden: true,
            minWidth: 140
          },
          {
            label: "分账接收人名称",
            prop: "recRpiName",
            type: "input",
            overHidden: true,
            minWidth: 150
          },
          {
            label: "分账项目编号",
            searchLabel: "分账项目",
            prop: "projectCode",
            type: "input",
            search: true,
            searchOrder: 1,
            overHidden: true,
            minWidth: 120
          },
          {
            label: "分账项目名称",
            prop: "projectName",
            type: "select",
            overHidden: true,
            minWidth: 150
          },
          {
            label: "通道简称",
            prop: "aisleKind",
            type: "select",
            search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=rbp_aisle_kind",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            searchOrder: 3,
            hide: true,
            dataType: "string",
            showColumn: false,
          },
          {
            label: "通道名称",
            prop: "aisleCode",
            type: "select",
            props: {
              label: "aisleName",
              value: "aisleCode",
            },
            searchOrder: 4,
            display: false,
            search: true,
            clearable: true,
            hide: true,
            rules: [
              {
                required: true,
                message: "请选择通道名称",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "服务商编号",
            searchLabel: "服务商",
            prop: "serviceNo",
            type: "select",
            search: true,
            searchOrder: 5,
            hide: true,
            overHidden: true,
            rules: [
              {
                required: true,
                message: "请选择服务商",
                trigger: "blur",
              },
            ],
          },
          {
            label: "",
            prop: "isSelectChild",
            type: "select",
            multiple: false,
            dataType: "string",
            showColumn: false,
            hide: true,
            search: true,
            searchType: "checkbox",
            searchLabelWidth: "0",
            searchOrder: 6,
            dicData: [
              {
                label: "包含下级",
                value: 1,
              },
            ],
          },
          {
            label: "商户编号",
            searchLabel: "商户",
            prop: "rpiNo",
            type: 'select',
            width: 140,
            search: true,
            overHidden: true,
            searchOrder: 7
          },
          {
            label: "商户名称",
            prop: "rpiName",
            type: "input",
            width: 150,
            overHidden: true
          },
          {
            label: "注册名称",
            prop: "registeredName",
            type: "input",
            search: true,
            searchOrder: 8,
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入注册名称",
                trigger: "blur",
              },
            ]
          },
          {
            label: "门店编号",
            prop: "rpsNo",
            type: "select",
            minWidth: 140,
            overHidden: true
          },
          {
            label: "门店名称",
            prop: "rpsName",
            type: "input",
            search: true,
            searchOrder: 9,
            minWidth: 150,
            overHidden: true
          },
          {
            label: "收银员名称",
            prop: "rpstName",
            type: "input",
            search: true,
            searchOrder: 10,
            minWidth: 150,
            overHidden: true
          },
          {
            label: "订单类型",
            prop: "divTradeType",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=div_trade_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            search: true,
            hide: true,
            searchOrder: 11,
            overHidden: true,
            minWidth: 100
          },
          {
            label: "订单类型",
            prop: "divTradeTypeName",
            display: false
          },
          {
            label: "订单分类",
            prop: "isPos",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=is_pos",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            search: true,
            searchOrder: 12,
            hide: true,
            overHidden: true,
            minWidth: 100
          },
          {
            label: "订单分类",
            prop: "isPosName",
            display: false
          },
          {
            label: "交易订单号",
            prop: "rptOrderCode",
            type: "input",
            overHidden: true,
            search: true,
            searchOrder: 13,
            minWidth: 180
          },
          {
            label: "分账订单号",
            prop: "divOrderCode",
            type: "input",
            overHidden: true,
            search: true,
            searchOrder: 14,
            minWidth: 180
          },
          {
            label: "分账流水号",
            prop: "divSerialCode",
            type: "input",
            overHidden: true,
            minWidth: 180
          },
          {
            label: "订单金额",
            prop: "rptAmount",
            type: "input",
            overHidden: true,
            minWidth: 140
          },
          {
            label: "交易状态",
            prop: "divPayState",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=div_pay_state",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            search: true,
            searchOrder: 17,
            hide: true,
            overHidden: true,
            minWidth: 100
          },
          {
            label: "交易状态",
            prop: "divPayStateName",
            display: false
          },
          {
            label: "分账模式",
            prop: "divisionMode",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=division_mode",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            search: true,
            searchOrder: 18,
            overHidden: true,
            hide: true,
            minWidth: 120
          },
          {
            label: "分账模式",
            prop: "divisionModeName",
            display: false
          },
          // {
          //   label: "分账模式",
          //   prop: "divisionPattern",
          //   type: "select",
          //   dicUrl: "/api/blade-system/dict/dictionary?code=division_pattern",
          //   props: {
          //     label: "dictValue",
          //     value: "dictKey",
          //   },
          //   search: true,
          //   searchOrder: 12,
          //   overHidden: true,
          //   minWidth: 120
          // },
          // {
          //   label: "分账模式",
          //   prop: "divisionPatternName",
          //   hide: true,
          //   display: false
          // },
          
          {
            label: "分账金额",
            prop: "divisionAmount",
            type: "input",
            overHidden: true,
            minWidth: 100
          },
          {
            label: "分账手续费",
            prop: "divisionCharge",
            type: "input",
            overHidden: true,
            minWidth: 100
          },
          {
            label: "实际分账金额",
            prop: "divisionIntoAmount",
            type: "input",
            overHidden: true,
            minWidth: 120
          },
          {
            label: "分账比例",
            prop: "divisionRate",
            type: "input",
            overHidden: true,
            minWidth: 100
          },
          {
            label: "分账类型",
            prop: "orderDivType",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=order_div_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            search: true,
            searchOrder: 15,
            hide: true,
            overHidden: true,
            minWidth: 120
          },
          {
            label: "分账类型",
            prop: "orderDivTypeName",
            display: false
          },
          {
            label: "分账状态",
            prop: "divisionState",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=division_state",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            search: true,
            searchOrder: 16,
            hide: true,
            overHidden: true,
            minWidth: 100
          },
          {
            label: "分账状态",
            prop: "divisionStateName",
            display: false
          },
          {
            label: "分账结果",
            prop: "divisionResult",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=division_result",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            search: true,
            searchOrder: 19,
            hide: true,
            overHidden: true,
            minWidth: 120
          },
          {
            label: "分账结果",
            prop: "divisionResultName",
            display: false
          },
          {
            label: "终端编号",
            prop: "rtNo",
            type: "input",
            overHidden: true,
            search: true,
            searchOrder: 20,
            minWidth: 180
          },
          {
            label: "设备序列号",
            prop: "rsSn",
            type: "input",
            overHidden: true,
            search: true,
            searchOrder: 21,
            minWidth: 180
          },
          {
            label: "交易日期",
            prop: "rptPayTime",
            type: "input",
            overHidden: true,
            width: 140,
          },
          {
            label: "交易日期",
            prop: "rptPayTimeData",
            type: "datetimerange",
            searchRange: true,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "开始时间",
            endPlaceholder: "截至时间",
            search: true,
            rules: [
              {
                required: true,
                message: "请选择交易日期",
                trigger: "blur",
              },
            ],
            hide: true,
            display: false,
          },
          {
            label: "分账日期",
            prop: "divTime",
            type: "input",
            overHidden: true,
            width: 140,
          },
          {
            label: "分账日期",
            prop: "divTimeData",
            type: "datetimerange",
            searchRange: true,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "开始时间",
            endPlaceholder: "截至时间",
            search: true,
            rules: [
              {
                required: true,
                message: "请选择分账日期",
                trigger: "blur",
              },
            ],
            hide: true,
            display: false,
          }
        ]
      },
      data: [],
      countData: {},
      selectType: '', // 1:本级 2:机构 3:服务商
      simQueryType: '' // 流量卡查询类型 1.明细 2.流水
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.active_flowCard_view, false),
        exportBtn: this.vaildData(this.permission.active_flowCard_export,false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  watch: {
    "search.rptPayTimeData"(val){
      if(val){
        this.search.rptPayTimeStart = val[0];
        this.search.rptPayTimeEnd = val[1];
      }else{
        this.search.rptPayTimeStart = "";
        this.search.rptPayTimeEnd = "";
      }
    },
    "search.divTimeData"(val){
      if(val){
        this.search.divTimeStart = val[0];
        this.search.divTimeEnd = val[1];
      }else{
        this.search.divTimeStart = "";
        this.search.divTimeEnd = "";
      }
    }
  },
  methods: {
    formatAmount(amount) {
      if (amount === undefined || amount === null || amount === '') {
        return '0.00';
      }
      return parseFloat(amount).toFixed(2);
    },
    setTime(type) {
      let date = new Date();
      this.query = {};
      if (type == "今天") {
        this.search.rptPayTimeStart = dateFormat(date, "yyyy-MM-dd") +' 00:00:00';
        this.search.rptPayTimeEnd = dateFormat(date, "yyyy-MM-dd") +' 23:59:59';
        this.page.currentPage = 1;
        this.forInSearch()
        this.refreshChange();
      } else if (type == "昨天") {
        this.search.rptPayTimeStart = dateFormat(new Date(date.getTime() - 3600 * 1000 * 24 * 1),"yyyy-MM-dd")+' 00:00:00';
        this.search.rptPayTimeEnd = dateFormat(new Date(date.getTime() - 3600 * 1000 * 24 * 1),"yyyy-MM-dd")+' 23:59:59';
        this.page.currentPage = 1;
        this.forInSearch()
        this.refreshChange();
      } else {
        // "本月"
        this.search.rptPayTimeStart = dateFormat(date,"yyyy-MM")+'-01 00:00:00';
        this.search.rptPayTimeEnd = dateFormat(date,"yyyy-MM-dd")+' 23:59:59';
        this.page.currentPage = 1;
        this.forInSearch()
        this.refreshChange();
      }
    },
    forInSearch(){
      for(let key in this.search){
        //console.log(key)
        if(this.search[key]){
          if(key.indexOf("$") == -1 ){
            this.query[key] = this.search[key]
          }
        }
      }
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        //   getDetail(this.form.id).then(res => {
        //     this.form = res.data.data;
        //   });
      }
      done();
    },
    searchReset() {
      // this.$set(this.search, 'timeData', ["",""])
      this.query = {};
      // this.search = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      // console.log(111111111)
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      // this.refreshChange()
      this.onLoad(this.page, this.query);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      // this.refreshChange()
      this.onLoad(this.page, this.query);
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      let addObj = {
        selectType: this.selectType,
        simQueryType:this.simQueryType
      };
      /* 表格 */
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, addObj)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });

      /* 获取统计 */
      getListCount(Object.assign(params, this.query, addObj)).then((res) => {
        this.countData = res.data.data;
      });
    },
    handleView(row) {
      this.$router.push({
        name: "分账订单详情",
        query: {
          d: this.$crypto.encrypt(
            JSON.stringify({
              orderId: row.id,
            })
          ),
        },
      });
    },
    handleExport() {
      let params = {
        ...this.search,
        current: "-1",
        size: "-1",
      };
      params.selectType = this.selectType;
      params.simQueryType = this.simQueryType;

      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.forInSearch()
        this.query.selectType = this.selectType;
        this.query.simQueryType = this.simQueryType;
        getExport(this.query).then((res) => {
            // downloadFileExcel(res);
            this.$message({
              type: "success",
              message: res.data.msg
            });
          },
          (error) => {
            window.console.log(error);
          }
        );
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    }
  }
};
</script>

<style lang="scss" scoped>
.curd-foter {
  padding-top: 10px;
  display: flex;
  flex-wrap: wrap;
}
::v-deep .avue-crud__menu {
  display: block;
}
::v-deep .avue-crud__right {
  display: flex;
  justify-content: flex-end !important;
}

.countcard {
  display: grid;
	grid-template-columns: repeat(6, 15%);
	gap: 16px;
  // width: 100vh;
  padding-top: 10px;
    .countcard-box {
    padding: 10px 0;
    background-color: #ffffff;
    border-radius: 10px;
    margin-bottom: 30px;
    box-shadow: 0px 2px 10px 0px rgba(83,136,233,0.15);
    .countcard-box-title {
      text-align: center;
      font-weight: bold;
      border-bottom: 1px solid rgba(214, 215, 217, 0.2);
      padding-bottom: 10px;
      font-size: 13px;
    }
    .countcard-box-content {
      padding-top: 10px;
      display: flex;
      align-items: center;
      justify-content: space-around;
      .countcard-box-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        .countcard-box-item-title {
          font-size: 12px;
          color: #666666;
          margin-bottom: 8px;
        }
        .countcard-box-item-number {
          font-size: 15px;
          font-weight: bold;
          color: #409EFF;
        }
      }
    }
  }
}
.redFont {
  color: red;
}
</style>